refactor: migrate prefer_match_file_name#305
Open
solid-illiaaihistov wants to merge 4 commits into
Open
Conversation
added 3 commits
June 26, 2026 16:08
…ssions and centralized parameter initialization
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
Collaborator
Author
|
/gemini review |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
Collaborator
Author
|
/gemini review |
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
|
||
| bool _doNormalizedNamesMatch(String path, String identifierName) { | ||
| /// Checks if the normalized file path matches the normalized identifier name. | ||
| bool doNormalizedNamesMatch(String path, String identifierName) { |
Contributor
There was a problem hiding this comment.
Why don't we move all of those 3 methods to visitor instead? Feels weird that we have this cross dependency between the two.
| @override | ||
| void visitClassDeclaration(ClassDeclaration node) { | ||
| super.visitClassDeclaration(node); | ||
| void visitCompilationUnit(CompilationUnit node) { |
Contributor
There was a problem hiding this comment.
void visitCompilationUnit(CompilationUnit node) {
final declarations = node.declarations
.whereNot(excludedEntities.shouldIgnoreEntity)
.map(
(d) {
final token = switch (d) {
ClassDeclaration() => d.namePart.typeName,
ExtensionDeclaration() => d.name,
MixinDeclaration() => d.name,
EnumDeclaration() => d.namePart.typeName,
ExtensionTypeDeclaration() => d.primaryConstructor.typeName,
_ => null,
};
return token == null ? null : (token: token, parent: d);
},
)
.nonNulls
.multiSortedBy(
[
(t) => Identifier.isPrivateName(t.token.lexeme) ? 1 : 0,
(t) => t.token.offset,
],
);
if (declarations.isEmpty) return;
final firstDeclaration = declarations.first;
final fullName = context.currentUnit?.file.path;
if (fullName != null &&
rule.doNormalizedNamesMatch(
fullName,
firstDeclaration.token.lexeme,
)) {
return;
}
final nodeType = humanReadableNodeType(
firstDeclaration.parent,
).toLowerCase();
final reporter = context.currentUnit?.diagnosticReporter;
reporter?.atToken(
firstDeclaration.token,
rule.diagnosticCode,
arguments: [nodeType],
);
}To add to utils:
extension<T> on Iterable<T> {
List<T> multiSortedBy(List<int Function(T)> keys) => sorted(
(a, b) =>
keys
.map((key) => key(a).compareTo(key(b)))
.firstWhereOrNull((comparison) => comparison != 0) ??
0,
);
}…ity to improve PreferMatchFileName rule encapsulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #258